package cn.llming.auth.controller;


import cn.llming.auth.domain.RoleMenu;
import cn.llming.auth.query.RoleMenuQuery;
import cn.llming.auth.service.IRoleMenuService;
import cn.llming.auth.util.BasePermission;
import cn.llming.basic.query.BaseQuery;
import cn.llming.basic.result.BaseResult;
import cn.llming.org.domain.Employee;
import cn.llming.vo.result.AjaxResult;
import cn.llming.vo.result.BaseMap;
import cn.llming.vo.result.ListPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
@BasePermission(name = "菜单管理",desc = "菜单管理权限")
public class MenuController {

    @Autowired
    private IRoleMenuService roleMenuService;


    //增加/修改菜单接口
    @PostMapping
    @BasePermission(name = "增加修改接口")
    public AjaxResult updateAndAddMenu(@RequestBody RoleMenu roleMenu ){
        try {
            if(Objects.nonNull(roleMenu.getId())){
                //修改菜单
                roleMenuService.update(roleMenu);
            }else{
                //新增菜单
                roleMenuService.insert(roleMenu);
            }
            //响应前段结果
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("保存失败"+e.getMessage()).setSuccess(false);
        }
    }

    //删除菜单接口
    @DeleteMapping("/{id}")
    @BasePermission(name = "删除接口")
    public AjaxResult deleteMenu(@PathVariable("id") Long id ){
        try {
            roleMenuService.delete(id);
            //响应前段结果
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("删除失败"+e.getMessage()).setSuccess(false);
        }
    }

    //查询单个菜单接口

    @GetMapping("/{id}")
    @BasePermission(name = "查询单个菜单接口")
    public AjaxResult loadMenuById(@PathVariable("id") Long id){
        try {
           RoleMenu roleMenu=roleMenuService.getById(id);
            //响应前段结果
            return AjaxResult.myRes().setObject(roleMenu);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询单个数据失败！"+e.getMessage()).setSuccess(false);
        }
    }

    //查询全部菜单接口
    @GetMapping
    @BasePermission(name = "查询全部菜单接口")
    public AjaxResult loadAllMenu(){
        try {
            List<RoleMenu> roleMenus=roleMenuService.getAll();
            //响应前段结果
            return AjaxResult.myRes().setObject(roleMenus);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询全部数据失败"+e.getMessage()).setSuccess(false);
        }
    }

    //批量删除菜单接口
    @PatchMapping
    @BasePermission(name = "批量删除菜单接口")
    public AjaxResult patchDeleteMenu(@RequestBody List<Long> ids){
        try {
            roleMenuService.batchDelete(ids);
            //响应前段结果
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("批量删除菜单数据失败"+e.getMessage()).setSuccess(false);
        }
    }


    //分页查询接口
    @PutMapping
    @BasePermission(name = "分页查询菜单接口")
    public AjaxResult getPageList(@RequestBody BaseQuery query){
        try {
           BaseResult<RoleMenu> rows =roleMenuService.getPageList(query);
            //响应前段结果
            return AjaxResult.myRes().setObject(rows);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("分页查询菜单数据失败"+e.getMessage()).setSuccess(false);
        }
    }

    //获取菜单树
    @GetMapping("/tree")
    @BasePermission(name = "获取菜单树接口")
    public AjaxResult getRoleMenuTree(){
        try {
            List<RoleMenu> menus=roleMenuService.getRoleMenuTree();
            return AjaxResult.myRes().setObject(menus);
        }catch (Exception e){
            return AjaxResult.myRes().setSuccess(false).setMessage("获取菜单树查询失败"+e.getMessage());
        }
    }

    //获取当前用户角色菜单
    @GetMapping("/menu_tree/{id}")

    public AjaxResult getEmployeeByMenus(@PathVariable("id") Long id){
        try {
//            String token = request.getHeader("token");
//            Employee employee=null;
//            if(Objects.nonNull(token)){
//                employee= (Employee)BaseMap.resultMap.get(token);
//            }

            List<RoleMenu> menus=roleMenuService.getEmployeeByMenus(id);
            return AjaxResult.myRes().setObject(menus);
        }catch (Exception e){
            return AjaxResult.myRes().setSuccess(false).setMessage("获取当前用户角色菜单查询失败"+e.getMessage());
        }
    }
}
